

function onScroll(callback) {
    const top = getAttr("scrollTop")
    const left = getAttr("scrollLeft")
    if (callback) {
      callback(top, left, getAttr("offsetHeight") - top - getAttr("clientHeight"))
    }
  }
  function getAttr(attr) {
    if (!utils.hasWindow) {
      return null;
    }
    return document.documentElement[attr] || document.body[attr];
  }
  
  function onResize(callback) {
    if (callback) {
      callback()
    }
  }
  
  const utils = {
    callbacks: {},
    get uid() {
      return Math.random().toString(36).slice(2);
    },
    get hasWindow() {
      return typeof window == "object";
    }
  }
  
  export default {
    clientHeight: getAttr("clientHeight"),
    getClientWidth: function () {
      return getAttr("clientWidth")
    },
    resize: function (callback) {
      if (!utils.hasWindow) {
        return
      }
      const resizefn = onResize.bind(null, callback);
      window.addEventListener("resize", resizefn, false)
      return resizefn
    },
    scroll: function (callback) {
      if (!utils.hasWindow) {
        return
      }
      const scrollfn = onScroll.bind(null, callback);
      window.addEventListener("scroll", scrollfn, false)
      scrollfn();
      return scrollfn
    },
    destroy: function (attr, fn) {
      if (!utils.hasWindow) {
        return
      }
      window.removeEventListener(attr, fn, false);
    }
  }